#!/bin/sh

HP_SUPPLIERS="1"
LP_CONSUMERS="1 10 100 200 300 500 1000"

for s in $HP_SUPPLIERS; do
  echo Supplier = $s

  for c in $LP_CONSUMERS; do
     x=`expr $c - 1`
     l=`printf %02.2d $x`
     echo Consumer = $c, LAST = $l

    echo DYN.LCL.S${s}.C${c}.log

    ./histo.pl -k 'Latency\[LCL,HP00' DYN.LCL.S${s}.C${c}.log >DYN.LCL.S${s}.C${c}.FIRST.histo
    ./histo.pl -k "Latency\[LCL,HP${l}" DYN.LCL.S${s}.C${c}.log >DYN.LCL.S${s}.C${c}.LAST.histo
    ./histo.pl -k "Latency\[LCL,HP" DYN.LCL.S${s}.C${c}.log >DYN.LCL.S${s}.C${c}.histo
  done
done

for f in DYN.*.histo; do
  b=`basename $f .histo`
  tail +3 $f | sort -n > ${b}.data
done

for i in LCL; do
  grep Min DYN.${i}.*.FIRST.histo |
    sed -e "s/DYN.$i.S//" -e 's/\.C/ /' -e 's/.FIRST.histo:/ /' -e 's/\,/ /g' |
    awk '{print $1, $2, $4, $6, $8, $10}' |
    sort -k 1n -k 2n > DYN.${i}.FIRST.tbl
  grep Min DYN.${i}.*.LAST.histo |
    sed -e "s/DYN.$i.S//" -e 's/\.C/ /' -e 's/.LAST.histo:/ /' -e 's/\,/ /g' |
    awk '{print $1, $2, $4, $6, $8, $10}' |
    sort -k 1n -k 2n > DYN.${i}.LAST.tbl
  paste DYN.${i}.FIRST.tbl DYN.${i}.LAST.tbl |
    awk '{print $1, $2, $3, $4, $5, $6, $9, $10, $11, $12}' > DYN.${i}.tbl
done

for i in LCL; do
  grep Min `ls DYN.${i}.*.histo | grep -v FIRST | grep -v LAST` |
    sed -e "s/DYN.$i.S//" -e 's/\.C/ /' -e 's/.histo:/ /' -e 's/\,/ /g' |
    awk '{print $1, $2, $4, $6, $8, $10}' |
    sort -k 1n -k 2n > DYN.${i}.tbl
done

for s in $HP_SUPPLIERS; do
  gnuplot <<_EOF_
    set grid xtics ytics
    set ylabel "Relative frequency"
    set xlabel "Latency (usecs) [$s suppliers, local events]"
    set terminal postscript eps color
    set output "DYN.LCL.S${s}.eps"
    plot 'DYN.LCL.S${s}.C1.data' w i
    set terminal x11
    plot 'DYN.LCL.S${s}.C1.data' w i
    pause 2
_EOF_
done

for i in DYN.*.eps; do
  b=`basename $i .eps`
  gs -sDEVICE=jpeg -g640x480 -r110x110 -sNOPAUSE \
      -sOutputFile="${b}.jpg" ${b}.eps quit.ps
done
